{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. NumPy数组算术"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组的值为：\n",
      " [[6 2 6]\n",
      " [9 8 3]]\n",
      "标量与数组组合：\n",
      " [[12  4 12]\n",
      " [18 16  6]]\n",
      "相同尺寸的数组运算：\n",
      " [[36  4 36]\n",
      " [81 64  9]]\n",
      "标量与数组的逻辑运算：\n",
      " [[ True False  True]\n",
      " [ True  True False]]\n",
      "同尺寸数组的逻辑运算：\n",
      " [[ True  True  True]\n",
      " [ True  True  True]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.random.randint(10, size=(2, 3))\n",
    "print(\"数组的值为：\\n\", arr1)\n",
    "print(\"标量与数组组合：\\n\", arr1 * 2)\n",
    "print(\"相同尺寸的数组运算：\\n\", arr1 * arr1)\n",
    "print(\"标量与数组的逻辑运算：\\n\", arr1 > 5)\n",
    "print(\"同尺寸数组的逻辑运算：\\n\", arr1 == arr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 广播的规则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组的值为：\n",
      " [[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "数组2的值为：\n",
      " [1 2 3]\n",
      "arr1的shape:\n",
      " (4, 3)\n",
      "arr2的shape:\n",
      " (3,)\n",
      "两数组相加的值为：\n",
      " [[ 1  3  5]\n",
      " [ 4  6  8]\n",
      " [ 7  9 11]\n",
      " [10 12 14]]\n",
      "arr4的值为:\n",
      " [[1]\n",
      " [2]\n",
      " [3]\n",
      " [4]]\n",
      "arr4的shape:\n",
      " (4, 1)\n",
      "arr5的值为：\n",
      " [[ 1  2  3]\n",
      " [ 5  6  7]\n",
      " [ 9 10 11]\n",
      " [13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.arange(12).reshape(4, 3)\n",
    "print(\"数组的值为：\\n\", arr1)\n",
    "arr2 = np.array([1, 2, 3])\n",
    "print(\"数组2的值为：\\n\", arr2)\n",
    "print(\"arr1的shape:\\n\", arr1.shape)\n",
    "print(\"arr2的shape:\\n\", arr2.shape)\n",
    "arr3 = arr1 + arr2\n",
    "print(\"两数组相加的值为：\\n\", arr3)  # 广播在丢失的轴上\n",
    "arr4 = np.arange(1, 5).reshape(4, 1)\n",
    "print(\"arr4的值为:\\n\", arr4)\n",
    "print(\"arr4的shape:\\n\", arr4.shape)\n",
    "arr5 = arr1 + arr4\n",
    "print(\"arr5的值为：\\n\", arr5)   # 广播在长度为1的轴上\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 标量广播\n",
    "假设你正在分析一个实验的数据arr = np.random.randint(10, size=(3,4))，目前数据发生了偏移，需要整体加5，请使用广播的特性将数字5加到数组的每个元素上，并打印结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr的数值为:\n",
      " [[7 6 4 6]\n",
      " [8 1 2 0]\n",
      " [6 0 8 9]]\n",
      "new_arr的结果为：\n",
      " [[12 11  9 11]\n",
      " [13  6  7  5]\n",
      " [11  5 13 14]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randint(10, size=(3, 4))\n",
    "print(\"arr的数值为:\\n\", arr)\n",
    "new_arr = arr + 5\n",
    "print(\"new_arr的结果为：\\n\", new_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 广播规则\n",
    "给定两个数组，arr1 = np.arange(12).reshape(4,3)和arr2 = np.array([1,2,3])。请使用广播的特性计算两个数组相加的结果，并打印。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1的数值为：\n",
      " [[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "arr2的数值为：\n",
      " [[1 2 3]]\n",
      "arr2的形状为：\n",
      " (1, 3)\n",
      "arr1+arr2的结果为：\n",
      " [[ 1  3  5]\n",
      " [ 4  6  8]\n",
      " [ 7  9 11]\n",
      " [10 12 14]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.arange(12).reshape(4, 3)\n",
    "arr2 = np.array([[1, 2, 3]])\n",
    "print(\"arr1的数值为：\\n\", arr1)\n",
    "print(\"arr2的数值为：\\n\", arr2)\n",
    "print(\"arr2的形状为：\\n\", arr2.shape)   # 缺少了一维\n",
    "print(\"arr1+arr2的结果为：\\n\", arr1 + arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 广播\n",
    "你有一组数据arr1 = np.arange(12).reshape(3,4)，现在希望在第一行数据上加2，在第二行数据上加4，第三行数据上加6。请使用广播的特性实现，并打印。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1的数值为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "arr2的数值为：\n",
      " [[2]\n",
      " [4]\n",
      " [6]]\n",
      "(3, 1)\n",
      "arr1+arr2的数值为：\n",
      " [[ 2  3  4  5]\n",
      " [ 8  9 10 11]\n",
      " [14 15 16 17]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.arange(12).reshape(3, 4)\n",
    "print(\"arr1的数值为：\\n\", arr1)\n",
    "arr2 = np.array([[2], [4], [6]])\n",
    "print(\"arr2的数值为：\\n\", arr2)     \n",
    "print(arr2.shape)       # 一个轴长度为1\n",
    "print(\"arr1+arr2的数值为：\\n\", arr1+arr2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python-data-analysis",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
